package com.wsfk.ssm.service;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import maps.com.wsfk.ssm.user.TempImageMapper;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.wsfk.ssm.entity.HandBook;
import com.wsfk.ssm.entity.Picture;
import com.wsfk.ssm.entity.TempImage;
import com.wsfk.ssm.entity.User;
import com.wsfk.ssm.path.CallPath;

@Service("TempImageSvcImpl")
public class TempImageSvcImpl implements TempImageSvc {
	Logger log = Logger.getLogger(this.getClass());
	
	FileSvc fileSvc;
	TempImageMapper tempImageMapper;


	public TempImageMapper getTempImageMapper() {
		return tempImageMapper;
	}
	/* (non-Javadoc)
	 * @see com.wsfk.ssm.service.TempImageSvc#setTempImageMapper(maps.com.wsfk.ssm.user.TempImageMapper)
	 */

	@Resource
	public void setTempImageMapper(TempImageMapper tempImageMapper) {
		this.tempImageMapper = tempImageMapper;
	}

	
	public FileSvc getFileSvc() {
		return fileSvc;
	}

	/* (non-Javadoc)
	 * @see com.wsfk.ssm.service.TempImageSvc#setFileSvc(com.wsfk.ssm.service.FileSvc)
	 */
	
	@Resource
	public void setFileSvc(FileSvc fileSvc) {
		this.fileSvc = fileSvc;
	}


	/* (non-Javadoc)
	 * @see com.wsfk.ssm.service.TempImageSvc#saveReadyFile(java.lang.String, java.lang.String, java.io.File)
	 */
	@Override
	public String saveReadyFile(String userId, String bookId, File imageFile){
		try {
			String imageId = UUID.randomUUID().toString();
			String imagePath = new CallPath().readyImageWay(userId,bookId,imageId).build();
			boolean f = fileSvc.saveImage(userId, bookId, imageId, imageFile);
			TempImage image = new TempImage();
			image.setId(imageId);
			image.setImageDir(imagePath);  
			image.setImageName(imageId+".jpg");
			image.setHandBookId(bookId);
			image.setUserId(userId);
			int i = tempImageMapper.addImage(image);
			return imageId;   
		} catch (IOException e) {
			log.error("No material picture"+e);
			throw new RuntimeException();
		}
	}
	
	
	/* (non-Javadoc)
	 * @see com.wsfk.ssm.service.TempImageSvc#saveBookBind(java.lang.String, com.wsfk.ssm.entity.HandBook, java.util.List, java.util.List)
	 */
	@Override
	public boolean saveBookBind(String userId,HandBook book, List<String> orders, List<String> imageIds) {
		try {
			User user = tempImageMapper.queryUserById(userId);
			HandBook handBook = tempImageMapper.queryBookById(book.getId());
			for(int i = 0 ; i < imageIds.size(); i++){
				String no =orders.get(i);
				String picPath = new CallPath().clearUpImageWay(user,handBook,no).build();
				TempImage tempImage = tempImageMapper.queryImageById(imageIds.get(i));
				fileSvc.savePic(user, handBook, tempImage, no);		
				Picture pic = new Picture();
				pic.setId(UUID.randomUUID().toString());
				pic.setFileDir(picPath);
				pic.setType(Picture.CONTENT);
				pic.setHandBookId(handBook.getId());
				int j = tempImageMapper.addPic(pic);
			}
			handBook.setName(book.getName());
			handBook.setAuthor(book.getAuthor());
			handBook.setCreateDate(new Date());
			handBook.setCreateStatus(HandBook.UNFINISHED);
			handBook.setVersion("1");
			handBook.setBookdir(new CallPath().bookWay(user, handBook).build());
			handBook.setId(book.getId());
			int k = tempImageMapper.updateBook(handBook);
			return true;
		} catch (IOException e) {
			log.error("No material picture"+e);
			throw new RuntimeException();
		}
	}
	
	/* (non-Javadoc)
	 * @see com.wsfk.ssm.service.TempImageSvc#saveIcon(java.lang.String, java.lang.String, java.io.File, java.lang.String)
	 */
	@Override
	public boolean saveIcon(String userId, String bookId, File file,
			String model) {
		try {
			User user = tempImageMapper.queryUserById(userId);
			HandBook book = tempImageMapper.queryBookById(bookId);
			String iconPath = new CallPath().iconWay(user, book).build();
			boolean fale = fileSvc.saveBook(user, book, file, model);
			Picture pic = new Picture();
			pic.setId(UUID.randomUUID().toString());
			pic.setType(Picture.ICON);
			pic.setFileDir(iconPath);
			pic.setHandBookId(bookId);
			tempImageMapper.addPic(pic);
			return true;
		} catch (IOException e) {
			log.error("File upload problems"+e);
			throw new RuntimeException();
		}
	}

	
	
}
